home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / indent.el < prev    next >
Lisp/Scheme  |  1993-03-22  |  8KB  |  257 lines

  1. ;;; indent.el --- indentation commands for Emacs
  2.  
  3. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; Commands for making and changing indentation in text.  These are
  26. ;; described in the Emacs manual.
  27.  
  28. ;;; Code:
  29.  
  30. (defvar indent-line-function 'indent-to-left-margin "\
  31. Function to indent current line.")
  32.  
  33. (defun indent-according-to-mode ()
  34.   "Indent line in proper way for current major mode."
  35.   (interactive)
  36.   (funcall indent-line-function))
  37.  
  38. (defun indent-for-tab-command ()
  39.   "Indent line in proper way for current major mode."
  40.   (interactive)
  41.   (if (eq indent-line-function 'indent-to-left-margin)
  42.       (insert-tab)
  43.     (funcall indent-line-function)))
  44.  
  45. (defun insert-tab ()
  46.   (if abbrev-mode
  47.       (expand-abbrev))
  48.   (if indent-tabs-mode
  49.       (insert ?\t)
  50.     (indent-to (* tab-width (1+ (/ (current-column) tab-width))))))
  51.  
  52. (defun indent-rigidly (start end arg)
  53.   "Indent all lines starting in the region sideways by ARG columns.
  54. Called from a program, takes three arguments, START, END and ARG."
  55.   (interactive "r\np")
  56.   (save-excursion
  57.     (goto-char end)
  58.     (setq end (point-marker))
  59.     (goto-char start)
  60.     (or (bolp) (forward-line 1))
  61.     (while (< (point) end)
  62.       (let ((indent (current-indentation)))
  63.     (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  64.     (or (eolp)
  65.         (indent-to (max 0 (+ indent arg)) 0)))
  66.       (forward-line 1))
  67.     (move-marker end nil)))
  68.  
  69. ;; This is the default indent-line-function,
  70. ;; used in Fundamental Mode, Text Mode, etc.
  71. (defun indent-to-left-margin ()
  72.   (or (= (current-indentation) left-margin)
  73.       (let (epos)
  74.     (save-excursion
  75.      (beginning-of-line)
  76.      (delete-region (point)
  77.             (progn (skip-chars-forward " \t")
  78.                    (point)))
  79.      (indent-to left-margin)
  80.      (setq epos (point)))
  81.     (if (< (point) epos)
  82.         (goto-char epos)))))
  83.  
  84. (defvar indent-region-function nil
  85.   "Function which is short cut to indent region using indent-according-to-mode.
  86. A value of nil means really run indent-according-to-mode on each line.")
  87.  
  88. (defun indent-region (start end arg)
  89.   "Indent each nonblank line in the region.
  90. With no argument, indent each line using indent-according-to-mode.
  91. \(If there is a fill prefix, make each line start with the fill prefix.)
  92. With argument COLUMN, indent each line to that column.
  93. Called from a program, takes three args: START, END and COLUMN."
  94.   (interactive "r\nP")
  95.   (if (null arg)
  96.       (if fill-prefix
  97.       (save-excursion
  98.         (goto-char end)
  99.         (setq end (point-marker))
  100.         (goto-char start)
  101.         (let ((regexp (regexp-quote fill-prefix)))
  102.         (while (< (point) end)
  103.           (or (looking-at regexp)
  104.           (insert fill-prefix))
  105.           (forward-line 1))))
  106.     (if indent-region-function
  107.         (funcall indent-region-function start end)
  108.       (save-excursion
  109.       (goto-char end)
  110.       (setq end (point-marker))
  111.       (goto-char start)
  112.       (or (bolp) (forward-line 1))
  113.       (while (< (point) end)
  114.         (funcall indent-line-function)
  115.         (forward-line 1))
  116.       (move-marker end nil))))
  117.     (setq arg (prefix-numeric-value arg))
  118.     (save-excursion
  119.       (goto-char end)
  120.       (setq end (point-marker))
  121.       (goto-char start)
  122.       (or (bolp) (forward-line 1))
  123.       (while (< (point) end)
  124.     (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  125.     (or (eolp)
  126.     (indent-to arg 0))
  127.     (forward-line 1))
  128.       (move-marker end nil))))
  129.  
  130. (defun indent-relative-maybe ()
  131.   "Indent a new line like previous nonblank line."
  132.   (interactive)
  133.   (indent-relative t))
  134.  
  135. (defun indent-relative (&optional unindented-ok)
  136.   "Space out to under next indent point in previous nonblank line.
  137. An indent point is a non-whitespace character following whitespace.
  138. If the previous nonblank line has no indent points beyond the
  139. column point starts at, `tab-to-tab-stop' is done instead."
  140.   (interactive "P")
  141.   (if abbrev-mode (expand-abbrev))
  142.   (let ((start-column (current-column))
  143.     indent)
  144.     (save-excursion
  145.       (beginning-of-line)
  146.       (if (re-search-backward "^[^\n]" nil t)
  147.       (let ((end (save-excursion (forward-line 1) (point))))
  148.         (move-to-column start-column)
  149.         ;; Is start-column inside a tab on this line?
  150.         (if (> (current-column) start-column)
  151.         (backward-char 1))
  152.         (or (looking-at "[ \t]")
  153.         unindented-ok
  154.         (skip-chars-forward "^ \t" end))
  155.         (skip-chars-forward " \t" end)
  156.         (or (= (point) end) (setq indent (current-column))))))
  157.     (if indent
  158.     (let ((opoint (point-marker)))
  159.       (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  160.       (indent-to indent 0)
  161.       (if (> opoint (point))
  162.           (goto-char opoint))
  163.       (move-marker opoint nil))
  164.       (tab-to-tab-stop))))
  165.  
  166. (defvar tab-stop-list
  167.   '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
  168.   "*List of tab stop positions used by `tab-to-tab-stops'.")
  169.  
  170. (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.")
  171. (if edit-tab-stops-map
  172.     nil
  173.   (setq edit-tab-stops-map (make-sparse-keymap))
  174.   (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
  175.   (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
  176.  
  177. (defvar edit-tab-stops-buffer nil
  178.   "Buffer whose tab stops are being edited--in case
  179. the variable `tab-stop-list' is local in that buffer.")
  180.  
  181. (defun edit-tab-stops ()
  182.   "Edit the tab stops used by `tab-to-tab-stop'.
  183. Creates a buffer *Tab Stops* containing text describing the tab stops.
  184. A colon indicates a column where there is a tab stop.
  185. You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
  186.   (interactive)
  187.   (setq edit-tab-stops-buffer (current-buffer))
  188.   (switch-to-buffer (get-buffer-create "*Tab Stops*"))
  189.   (use-local-map edit-tab-stops-map)
  190.   (make-local-variable 'indent-tabs-mode)
  191.   (setq indent-tabs-mode nil)
  192.   (overwrite-mode 1)
  193.   (setq truncate-lines t)
  194.   (erase-buffer)
  195.   (let ((tabs tab-stop-list))
  196.     (while tabs
  197.       (indent-to (car tabs) 0)
  198.       (insert ?:)
  199.       (setq tabs (cdr tabs))))
  200.   (let ((count 0))
  201.     (insert ?\n)
  202.     (while (< count 8)
  203.       (insert (+ count ?0))
  204.     (insert "         ")
  205.       (setq count (1+ count)))
  206.     (insert ?\n)
  207.     (while (> count 0)
  208.       (insert "0123456789")
  209.       (setq count (1- count))))
  210.   (insert "\nTo install changes, type C-c C-c")
  211.   (goto-char (point-min)))
  212.  
  213. (defun edit-tab-stops-note-changes ()
  214.   "Put edited tab stops into effect."
  215.   (interactive)
  216.     (let (tabs)
  217.       (save-excursion
  218.     (goto-char 1)
  219.     (end-of-line)
  220.     (while (search-backward ":" nil t)
  221.       (setq tabs (cons (current-column) tabs))))
  222.       (bury-buffer (prog1 (current-buffer)
  223.               (switch-to-buffer edit-tab-stops-buffer)))
  224.       (setq tab-stop-list tabs))
  225.   (message "Tab stops installed"))
  226.  
  227. (defun tab-to-tab-stop ()
  228.   "Insert spaces or tabs to next defined tab-stop column.
  229. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  230. Use \\[edit-tab-stops] to edit them interactively."
  231.   (interactive)
  232.   (if abbrev-mode (expand-abbrev))
  233.   (let ((tabs tab-stop-list))
  234.     (while (and tabs (>= (current-column) (car tabs)))
  235.       (setq tabs (cdr tabs)))
  236.     (if tabs
  237.     (indent-to (car tabs))
  238.       (insert ?\ ))))
  239.  
  240. (defun move-to-tab-stop ()
  241.   "Move point to next defined tab-stop column.
  242. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  243. Use \\[edit-tab-stops] to edit them interactively."
  244.   (interactive)
  245.   (let ((tabs tab-stop-list))
  246.     (while (and tabs (>= (current-column) (car tabs)))
  247.       (setq tabs (cdr tabs)))
  248.     (if tabs
  249.     (move-to-column (car tabs) t))))
  250.  
  251. (define-key global-map "\t" 'indent-for-tab-command)
  252. (define-key esc-map "\034" 'indent-region)
  253. (define-key ctl-x-map "\t" 'indent-rigidly)
  254. (define-key esc-map "i" 'tab-to-tab-stop)
  255.  
  256. ;;; indent.el ends here
  257.